/*
 *   Copyright 2009 Sukharev Eduard <kraplax@mail.ru>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Library General Public License as
 *   published by the Free Software Foundation; either version 2 or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details
 *
 *   You should have received a copy of the GNU Library General Public
 *   License along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "plasma-welinux.h"
#include "updateswidget.h"

#include <QtDebug>
#include <QGraphicsLinearLayout>
#include <QGridLayout>
#include <QSpinBox>
#include <QLabel>
#include <KConfigDialog>
#include <KStandardDirs>
 
#include <plasma/widgets/tabbar.h>

////////////////////////////////////////////////////////////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////////////////////////////////////////////////////////////

WelinuxApplet::WelinuxApplet(QObject *parent, const QVariantList &args)
    : Plasma::PopupApplet(parent, args),
    m_graphicsWidget(0) //added so that there's no widget at the start and we won't end up with two overlayed widgets
{
    setPopupIcon("bookmark-new-list");
    setBackgroundHints(DefaultBackground);
    setHasConfigurationInterface(true);
    
    resize(300, 350);
    setAspectRatioMode(Plasma::Square);
};

WelinuxApplet::~WelinuxApplet()
{
    if(hasFailedToLaunch()){
        qDebug() << "Emergency exit!";
        //some cleanup (?)
    } else {
        qDebug() << "Everything was fine, exiting and saving config to file";
        //if(){
            KConfigGroup configGrp = config();
            saveState(configGrp);
        //};
        //saving settings
    };
};

void WelinuxApplet::init()
{
    readConfig();
};

QGraphicsWidget * WelinuxApplet::graphicsWidget()
{
    if(!m_graphicsWidget){ //we don't want multiple overlayed widgets in one applet
    
    m_graphicsWidget = new QGraphicsWidget(this);
    Plasma::TabBar * tabs = new Plasma::TabBar(m_graphicsWidget);

    UpdatesWidget * posts = new UpdatesWidget(0, 0, Posts);
    posts->setMaxFetched(m_maxFetched);
    UpdatesWidget * comments = new UpdatesWidget(0, 0, Comments);
    comments->setMaxFetched(m_maxFetched);
    //UpdatesWidget * mixed = new UpdatesWidget(0, 0, Mixed);

    tabs->addTab(i18n("Posts"), posts);
    tabs->addTab(i18n("Comments"), comments);
    //tabs->addTab(i18n("Mixed"), mixed);
    
    QSizePolicy expanding_policy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    QSizePolicy minimal_policy(QSizePolicy::Expanding, QSizePolicy::Minimum);
    };

    m_graphicsWidget->setMinimumSize(255, 200); // set the size when the applet collapses to popup icon
    return m_graphicsWidget;
};

void WelinuxApplet::createConfigurationInterface(KConfigDialog *parent)
{
    QWidget * config = new QWidget(0);
    QGridLayout * mainLayout = new QGridLayout(config);
    
    QLabel * lblMaxFetched = new QLabel(i18n("Nuber of fetched items"));
    lblMaxFetched->setAlignment(Qt::AlignVCenter);

    QSpinBox * spinMaxFetched = new QSpinBox();
    spinMaxFetched->setRange(3,50);
    spinMaxFetched->setValue(8);
    spinMaxFetched->setSingleStep(1);
    
    mainLayout->addWidget(lblMaxFetched,0,0,Qt::AlignRight);
    mainLayout->addWidget(spinMaxFetched,0,1,Qt::AlignLeft);
    parent->addPage(config, i18n("Welinux Settings"), "rating");
};

void WelinuxApplet::readConfig()
{
    KConfigGroup configGrp = config();

    m_maxFetched = configGrp.readEntry("MaxFetchedItems", 8);
    //graphicsWidget()->
    //m_color = configGrp.readEntry("fontcolor", );
};
void WelinuxApplet::writeConfig()
{
    KConfigGroup configGrp = config();

    configGrp.writeEntry("MaxFetchedItems", m_maxFetched);
    //saveState(configGrp);
    configGrp.sync();
};


#include "plasma-welinux.moc"
